package com.example.navigationdemo.nav.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.example.navigationdemo.R;
import com.example.navigationdemo.nav.model.BottomBar;
import com.example.navigationdemo.nav.model.Destination;
import com.example.navigationdemo.nav.utils.AppConfig;
import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;

import java.util.List;

/**
 * @author: tuyrt7 on 2020/9/3.
 */
public class AppBottomBar extends BottomNavigationView {

    private static final int[] sIcons = new int[]{
            R.drawable.icon_tab_home, R.drawable.icon_tab_msg, R.drawable.icon_tab_center,
            R.drawable.icon_tab_find, R.drawable.icon_tab_mine
    };

    private BottomBar mConfig;

    public AppBottomBar(Context context) {
        this(context,null);
    }

    public AppBottomBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    @SuppressLint("RestrictedApi")
    public AppBottomBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mConfig = AppConfig.getBottomBarConfig();

        int[][] state = new int[2][];
        state[0] = new int[]{android.R.attr.state_selected};
        state[1] = new int[]{};
        int[] colors = new int[]{Color.parseColor(mConfig.activeColor), Color.parseColor(mConfig.inActiveColor)};
        ColorStateList stateList = new ColorStateList(state, colors);
        setItemTextColor(stateList);
        //setItemIconTintList(stateList);

        setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
        List<BottomBar.Tab> tabs = mConfig.tabs;
        for (int i = 0; i < tabs.size(); i++) {
            BottomBar.Tab tab = tabs.get(i);
            if (!tab.enable) {
                continue;
            }

            int itemId = getItemId(tab.pageUrl);
            if (itemId < 0) {
                continue;
            }

            MenuItem menuItem = getMenu().add(0, itemId, tab.index, tab.title);
            menuItem.setIcon(sIcons[i]);
        }

        for (int i = 0; i < tabs.size(); i++) {
            BottomBar.Tab tab = tabs.get(i);
            if (!tab.enable) {
                continue;
            }

            int itemId = getItemId(tab.pageUrl);
            if (itemId < 0) {
                continue;
            }

            int iconSize = dp2px(tab.size);
            BottomNavigationMenuView menuView = (BottomNavigationMenuView) getChildAt(0);
            BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(i);

            itemView.setIconTintList(null);
            itemView.setIconSize(iconSize);

//            if (TextUtils.isEmpty(tab.title)) {
//                int tintColor = TextUtils.isEmpty(tab.tintColor) ? Color.parseColor("#ff678f") : Color.parseColor(tab.tintColor);
//                itemView.setIconTintList(null);
//                //禁止掉点击按时  上下浮动的效果
//                itemView.setShifting(false);
//
//
//                /**
//                 如果想要禁止掉所有按钮的点击浮动效果。
//                 那么还需要给选中和未选中的按钮配置一样大小的字号。
//                 在MainActivity布局的AppBottomBar标签增加如下配置，
//                 @style/active，@style/inActive 在style.xml中
//                 app:itemTextAppearanceActive="@style/active"
//                 app:itemTextAppearanceInactive="@style/inActive"
//                 */
//            }
        }

        if (mConfig.selectTab != 0) {
            BottomBar.Tab selectTab = mConfig.tabs.get(mConfig.selectTab);
            if (selectTab.enable) {
                int itemId = getItemId(selectTab.pageUrl);

                post(() -> setSelectedItemId(itemId));
            }
        }
    }

    private int dp2px(int dpValue) {
        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        return (int) (metrics.density * dpValue + 0.5f);
    }

    /**
     *  比对url，找出对应 destination 中 id 返回
     * @param pageUrl
     * @return
     */
    private int getItemId(String pageUrl) {
        Destination destination = AppConfig.getDestConfig().get(pageUrl);
        if (destination == null) {
            return -1;
        }
        return destination.id;
    }
}
